let createNewPromise = (url,data,method,promiseResolve)=>{
	return new Promise(() => {
		wx.request({
			url:url,
			data:data,
			method:method,
			header:{
				token: getApp().globalData.token
			},
			success:(res)=>{ 
				promiseResolve(res.data); 
			},
			fail:(err)=>{    
				console.log(err);
			},
		})
	})
}

let getResult = (url,params,method)=>{
	let promiseTemp = null;
	let promiseResolve = null;

	async function fun(){
	    promiseTemp = new Promise(resolve => promiseResolve = resolve); 
		createNewPromise(url,params,method,promiseResolve);
		let res = await promiseTemp;
		return res;
	}
	return fun()
}

//获取全部的班级列表
const getClassList = (current,userId)=>{
	let url = getApp().globalData.baseUrl + "class";
	let data = {
		size: getApp().globalData.size,
		current: current,
		userId: userId
	}
	return getResult(url,data,"GET");
}
//获取我加入的班级列表
const getMyClassList = (current)=>{
	let url = getApp().globalData.baseUrl + "class/added";
	let data = {
		size: getApp().globalData.size,
		current: current,
		userId: getApp().globalData.userInfo.id
	}
	return getResult(url,data,"GET");
}
//在所有的班级中查询
const searchClassList = (current,content)=>{
	let url = getApp().globalData.baseUrl + "class/select";
	let data = {
		size: getApp().globalData.size,
		current: current,
		content: content,
		userId: getApp().globalData.userInfo.id
	}
	return getResult(url,data,"GET");
}
//查询我的班级列表
const searchMyClassList = (current,content)=>{
	let url = getApp().globalData.baseUrl + "class/added/select";
	let data = {
		size: getApp().globalData.size,
		current: current,
		content: content,
		userId: getApp().globalData.userInfo.id
	}
	return getResult(url,data,"GET");
}
//加入班级
const joinClass = (code,userId,classId)=>{
	let url = getApp().globalData.baseUrl + "class";
	let data = {
		code:code,
		userId:userId,
		classId:classId
	}
	return getResult(url,data,"POST");
}
//退出班级
const exitClass = (classId)=>{
	console.log(classId);
	let url = getApp().globalData.baseUrl + "class?userClassId="+classId;
	let data = {}
	return getResult(url,data,"DELETE");
}

export default{
	getClassList,
	getMyClassList,
	searchClassList,
	searchMyClassList,
	joinClass,
	exitClass
}